import pytest, time
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.Pageobject.system_setting_page import SystemSettingPage
from Case.python_ui.Pageobject.login_pages import Loginpages
from Case.python_ui.Test_data.login_data import Logindata
from selenium import webdriver
from Case.python_ui.PageLocate.admin_enterprise_user_locator import AdminEnterpriseUserLocator
from Case.python_ui.PageLocate.admin_exporting_record_locator import AdminExportingRecordLocator
from selenium.common.exceptions import NoSuchElementException


class AdminExportingRecordModule(Basepage):

    # 判断是否包含元素
    def check_contain_str(self, string, except_string):
        if except_string in string:
            assert True
        else:
            assert False, '实际值:' + string + '期望值:' + except_string

    # 校验某个属性值是否符合预期
    def check_attribute(self, real_result, except_result):
        if real_result == except_result:
            assert True
        else:
            assert False, '实际值:' + real_result + '期望值:' + except_result

    # 元素是否存在
    def element_is_exist(self, locate):
        doc = '判断元素是否存在'
        try:
            self.get_element(locate, doc)
        except NoSuchElementException as e:
            return False
        else:
            return True

    # 进入order management->my resource菜单界面
    def enter_order_management_myresource(self):
        time.sleep(2)
        doc = "进入order management->my resource菜单界面"
        for i in range(3):
            try:
                eles = self.get_elements(AdminExportingRecordLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(AdminExportingRecordLocator.order_management_tab, doc)
                self.click_element(AdminExportingRecordLocator.order_management_tab, doc)
                self.wait_elevisble(AdminExportingRecordLocator.order_management_resource_tittle, doc)
                self.click_element(AdminExportingRecordLocator.order_management_resource_tittle, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(AdminExportingRecordLocator.order_management_resource_page, doc)

    # 查看my resource界面下载界面跳转校验
    def check_page_download_jump_func(self):
        time.sleep(5)
        doc = "查看my resource界面下载界面跳转校验"
        self.move_mouse_to_click_element(AdminExportingRecordLocator.order_management_resource_download_btn, doc)
        time.sleep(3)
        self.wait_elevisble(AdminExportingRecordLocator.my_resource_download_jump_page, doc)
        self.check_attribute(self.get_element(AdminExportingRecordLocator.my_resource_download_jump_page, doc).text, 'Export Records')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.my_resource_download_jump_file_name, doc).text, 'File Name')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.my_resource_download_jump_module, doc).text,  'Module')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.my_resource_download_jump_status, doc).text,  'Status')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.my_resource_download_jump_creation_time, doc).text,  'Creation Time')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.my_resource_download_jump_actions, doc).text, 'Actions')

    # 进入Order Management->Orders菜单界面
    def enter_order_management_orders_page(self):
        time.sleep(2)
        doc = "进入Order Management->Orders菜单界面"
        for i in range(3):
            try:
                eles = self.get_elements(AdminExportingRecordLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(AdminExportingRecordLocator.order_management_tab, doc)
                self.click_element(AdminExportingRecordLocator.order_management_tab, doc)
                self.wait_elevisble(AdminExportingRecordLocator.order_management_orders_tittle, doc)
                self.click_element(AdminExportingRecordLocator.order_management_orders_tittle, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(AdminExportingRecordLocator.order_management_orders_page, doc)

    # 查看orders界面下载界面跳转校验
    def check_orders_page_download_jump_func(self):
        time.sleep(5)
        doc = "查看orders界面下载界面跳转校验"
        self.move_mouse_to_click_element(AdminExportingRecordLocator.orders_date_input, doc)
        self.wait_elevisble(AdminExportingRecordLocator.orders_date_start_input, doc)
        self.move_mouse_to_click_element(AdminExportingRecordLocator.orders_date_start_input, doc)
        self.move_mouse_to_click_element(AdminExportingRecordLocator.orders_date_end_input, doc)
        time.sleep(2)
        self.move_mouse_to_click_element(AdminExportingRecordLocator.order_management_orders_download_btn, doc)
        time.sleep(3)
        self.wait_elevisble(AdminExportingRecordLocator.orders_download_jump_page, doc)
        self.check_attribute(self.get_element(AdminExportingRecordLocator.orders_download_jump_page, doc).text, 'Export Records')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.orders_download_jump_file_name, doc).text, 'File Name')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.orders_download_jump_module, doc).text,  'Module')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.orders_download_jump_status, doc).text,  'Status')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.orders_download_jump_creation_time, doc).text,  'Creation Time')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.orders_download_jump_actions, doc).text, 'Actions')

    # 进入Bill->Income/Expense菜单界面
    def enter_bill_income_expense_page(self):
        time.sleep(2)
        doc = "进入Bill->Income/Expense菜单界面"
        for i in range(3):
            try:
                eles = self.get_elements(AdminExportingRecordLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(AdminExportingRecordLocator.bill_tab, doc)
                self.click_element(AdminExportingRecordLocator.bill_tab, doc)
                self.wait_elevisble(AdminExportingRecordLocator.bill_income_expense_tittle, doc)
                self.click_element(AdminExportingRecordLocator.bill_income_expense_tittle, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(AdminExportingRecordLocator.bill_income_expense_page, doc)

    # 查看Income/Expense界面下载界面跳转校验
    def check_income_expense_page_download_jump_func(self):
        time.sleep(5)
        doc = "查看Income/Expense界面下载界面跳转校验"
        # 不选择时间时点击下载，界面会弹出提示语
        self.move_mouse_to_click_element(AdminExportingRecordLocator.bill_income_expense_download_btn, doc)
        self.check_attribute(self.get_element(AdminExportingRecordLocator.download_error_tips, doc).text, 'Please select start date and end date for export. The export time range cannot exceed 6 months.')
        # 选择时间之后，再次点击下载
        self.move_mouse_to_click_element(AdminExportingRecordLocator.income_expense_date_input, doc)
        self.wait_elevisble(AdminExportingRecordLocator.income_expense_date_start_input, doc)
        self.move_mouse_to_click_element(AdminExportingRecordLocator.income_expense_date_start_input, doc)
        self.move_mouse_to_click_element(AdminExportingRecordLocator.income_expense_date_end_input, doc)
        time.sleep(2)
        self.move_mouse_to_click_element(AdminExportingRecordLocator.bill_income_expense_download_btn, doc)
        time.sleep(3)
        self.wait_elevisble(AdminExportingRecordLocator.income_expense_download_jump_page, doc)
        self.check_attribute(self.get_element(AdminExportingRecordLocator.income_expense_download_jump_page, doc).text, 'Export Records')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.income_expense_download_jump_file_name, doc).text, 'File Name')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.income_expense_download_jump_module, doc).text,  'Module')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.income_expense_download_jump_status, doc).text,  'Status')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.income_expense_download_jump_creation_time, doc).text,  'Creation Time')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.income_expense_download_jump_actions, doc).text, 'Actions')

# 进入Bill->Billing Statement菜单界面
    def enter_billing_statement_page(self):
        time.sleep(2)
        doc = "进入Bill->Billing Statement菜单界面"
        for i in range(3):
            try:
                eles = self.get_elements(AdminExportingRecordLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(AdminExportingRecordLocator.bill_tab, doc)
                self.click_element(AdminExportingRecordLocator.bill_tab, doc)
                self.wait_elevisble(AdminExportingRecordLocator.bill_billing_statement_tittle, doc)
                self.click_element(AdminExportingRecordLocator.bill_billing_statement_tittle, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(AdminExportingRecordLocator.bill_billing_statement_page, doc)

    # 查看Billing Statement界面下载界面跳转校验
    def check_billing_statement_page_download_jump_func(self):
        time.sleep(5)
        doc = "查看Billing Statement界面下载界面跳转校验"
        self.move_mouse_to_click_element(AdminExportingRecordLocator.bill_billing_statement_download_btn, doc)
        time.sleep(3)
        self.wait_elevisble(AdminExportingRecordLocator.billing_statement_download_jump_page, doc)
        self.check_attribute(self.get_element(AdminExportingRecordLocator.billing_statement_download_jump_page, doc).text, 'Export Records')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.billing_statement_download_jump_file_name, doc).text, 'File Name')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.billing_statement_download_jump_module, doc).text,  'Module')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.billing_statement_download_jump_status, doc).text,  'Status')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.billing_statement_download_jump_creation_time, doc).text,  'Creation Time')
        self.check_attribute(self.get_element(AdminExportingRecordLocator.billing_statement_download_jump_actions, doc).text, 'Actions')

    # 查看Billing Statement的export record界面status功能校验
    def check_billing_statement_page_status_func(self):
        time.sleep(5)
        doc = "查看Billing Statement的export record界面status功能校验"
        self.move_mouse_to_click_element(AdminExportingRecordLocator.bill_billing_statement_download_btn, doc)
        time.sleep(2)
        self.wait_elevisble(AdminExportingRecordLocator.billing_statement_download_jump_page, doc)
        self.check_attribute(self.get_element(AdminExportingRecordLocator.billing_statement_download_status_generated, doc).text, 'Being Generated')
        time.sleep(2)
        if self.get_element(AdminExportingRecordLocator.billing_statement_download_jump_status_successful, doc).text == 'Export successful':
            self.check_attribute(self.get_element(AdminExportingRecordLocator.billing_statement_download_jump_status_successful, doc).text, 'Export successful')
        else:
            self.check_attribute(self.get_element(AdminExportingRecordLocator.billing_statement_download_jump_status_successful, doc).text, 'Export failed')

    # 查看orders界面下载Exporting Record界面任务生成失败status显示
    def check_orders_page_status_func(self):
        time.sleep(5)
        doc = "查看orders界面下载界面跳转校验"
        self.move_mouse_to_click_element(AdminExportingRecordLocator.orders_date_input, doc)
        self.wait_elevisble(AdminExportingRecordLocator.orders_date_start_input, doc)
        self.move_mouse_to_click_element(AdminExportingRecordLocator.orders_date_start_input, doc)
        self.move_mouse_to_click_element(AdminExportingRecordLocator.orders_date_end_input, doc)
        time.sleep(2)
        self.move_mouse_to_click_element(AdminExportingRecordLocator.order_management_orders_download_btn, doc)
        time.sleep(2)
        self.wait_elevisble(AdminExportingRecordLocator.orders_download_jump_page, doc)
        self.check_attribute(self.get_element(AdminExportingRecordLocator.orders_download_status_generated, doc).text, 'Being Generated')
        time.sleep(2)
        if self.get_element(AdminExportingRecordLocator.orders_download_jump_status_successful, doc).text == 'Export successful':
            self.check_attribute(self.get_element(AdminExportingRecordLocator.orders_download_jump_status_successful, doc).text, 'Export successful')
        else:
            self.check_attribute(self.get_element(AdminExportingRecordLocator.orders_download_jump_status_successful, doc).text, 'Export failed')
